package com.example.splash
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.animation.DecelerateInterpolator
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.RouterPath
import com.example.splash.databinding.ActivitySplashBinding
import com.gyf.immersionbar.ImmersionBar
import com.tencent.mmkv.MMKV
import com.zhpan.bannerview.adapter.OnPageChangeListenerAdapter
import com.zhpan.bannerview.holder.HolderCreator


class SplashActivity : AppCompatActivity() ,HolderCreator<BannerViewHolder>{

    private  var mHandler : Handler ? =null

    private var mDataBinding : ActivitySplashBinding? =null

    private var guidelist: List<GuideEntiy> = arrayListOf(
        GuideEntiy(imageRes = R.drawable.glide1, imageDesc = "在这里\n你可以听到周围人的心声", imageUrl = ""),
        GuideEntiy(imageRes = R.drawable.glide2, imageDesc = "在这里\nTA会在下一秒遇见你", imageUrl = ""),
        GuideEntiy(imageRes = R.drawable.glide3, imageDesc = "在这里\n不再错过可以改变你一生的人", imageUrl = ""),

    )
    override fun createViewHolder()= BannerViewHolder()



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDataBinding=DataBindingUtil.setContentView(this,R.layout.activity_splash)

        ImmersionBar.with(this@SplashActivity).init()

        mHandler = Handler(Looper.getMainLooper())
        mHandler?.postDelayed(this::startToMain,1000)

    }
    private fun startToMain(){
        var isFirst =MMKV.defaultMMKV().decodeBool("first",true)
        if(isFirst){
            mDataBinding?.guideLayout?.visibility =View.VISIBLE
            //初始化BannerViewPager组件
            mDataBinding?.bannerView
                ?.setAutoPlay(false)
                ?.setCanLoop(false)
                ?.setIndicatorMargin(0,0,0,100)
                ?.setIndicatorGap(10)
                ?.setIndicatorColor(Color.parseColor("#FFFFFF"),Color.parseColor("#363636"))
                ?.setIndicatorSlideMode(com.zhpan.bannerview.constants.IndicatorSlideMode.SMOOTH)
                ?.setIndicatorRadius(3)
                ?.setOnPageChangeListener(object : OnPageChangeListenerAdapter(){
                    override fun onPageSelected(position: Int) {
                        updataTextView(position)
                    }
                })
                ?.setHolderCreator(this@SplashActivity)
                ?.create(guidelist)

                MMKV.defaultMMKV().encode("first",false)
            mDataBinding?.btnStart?.setOnClickListener {
                ARouter.getInstance().build(RouterPath.PATH_MAIN).navigation(this@SplashActivity)
                finish()
            }

        }else{
            ARouter.getInstance().build(RouterPath.PATH_MAIN).navigation(this@SplashActivity)
            finish()

        }

    }

    private fun updataTextView(position: Int) {
        mDataBinding?.tvDescribe?.text=guidelist[position].imageDesc

        //定义动画
        val translateAnim = ObjectAnimator.ofFloat(mDataBinding?.tvDescribe, "translationX", -120f, 0f)
        translateAnim.duration = 1200
        translateAnim.interpolator = DecelerateInterpolator()

        val alphaAnim = ObjectAnimator.ofFloat(mDataBinding?.tvDescribe, "alpha", 0f, 1f)
        alphaAnim.duration = 1200
        val animSet =AnimatorSet()
        animSet.playTogether(translateAnim,alphaAnim)
        animSet.start()

        //滑到最后一页显示
        if(position==guidelist.size-1 && mDataBinding?.btnStart?.visibility==View.GONE){
            mDataBinding?.btnStart?.visibility=View.VISIBLE
            //透明度
            val alphaanim2=ObjectAnimator.ofFloat(mDataBinding?.btnStart,"alpha",0f,1f)
            alphaanim2.setDuration(1200)
            alphaanim2.start()
        }else{
            mDataBinding?.btnStart?.visibility=View.GONE
        }



    }

    override fun onDestroy() {
        super.onDestroy()
        mHandler?.removeCallbacksAndMessages(null)
        mHandler=null
    }


}